import { reactive } from "vue";
import { Api } from "../../../api/api.js";
function expenseDataJS() {
    let status = reactive({
        userinfo: JSON.parse(window.sessionStorage.getItem("userinfo")),
        initData: [],
        dayList: [],
        base: { year: [], mon: [], day: [] },
        selectDate: {
            year: "",
            mon: "",
            day: "",
        },
        details: [],
        yearTotal: "",
        monTotal: "",
        dayTotal: ""
    })
    let methodFun = {
        // 获取所有数据
        getAll: () => {
            Api.getexpense({ user: status.userinfo.user }).then((res) => {
                status.initData = res.data[0]?.item;
                methodFun.getYear();
            });
        },
        // 获取可选择的年份
        getYear: () => {
            status.initData?.forEach((item) => {
                status.base.year.push({ value: item.yearDate, label: item.yearDate });
            });
            console.log("可选择的年份有：", status.base.year);
        },
        // 年份改变
        changeYear: () => {
            let year = status.selectDate.year;
            status.initData.forEach((item) => {
                if (item.yearDate == year) {
                    status.yearTotal = item.yearTotal;
                    let monlist = item.yearDetails;
                    monlist.forEach((item1) => {
                        let mon = item1.monDate.split("-")[1];
                        status.base.mon.push({ value: mon, label: item1.mon });
                    })

                }
            })
        },
        //月份改变
        changemon: () => {
            let year = status.selectDate.year;
            let Smon = status.selectDate.mon;
            status.initData.forEach((item) => {
                if (item.yearDate == year) {
                    let monlist = item.yearDetails;
                    monlist.forEach((item1) => {
                        let mon = item1.monDate.split("-")[1];
                        if (mon == Smon) {
                            status.monTotal = item1.monTotal;
                            status.dayList = item1.monDetails;
                            console.log(status.dayList, "本月数据");
                            let daylist = item1.monDetails;
                            daylist.forEach((item2) => {
                                let day = item2.dayDate.split("-")[2];
                                status.base.day.push({ value: day, label: item1.day });
                            })
                        }
                    })
                }
            })
        },
        // 天数改变
        changeDay: () => {
            let newDay = status.selectDate.day;
            status.dayList.forEach((item) => {
                let day = item.dayDate.split("-")[2];
                if (newDay == day) {
                    status.dayTotal = item.dayTotal;
                    status.details = item.dayDetails;
                    console.log("查询的数据：", status.details);
                }
            })
            console.log(status.selectDate.day, 123);
        }
    }
    return {
        status,
        methodFun
    }
}

export default expenseDataJS;